using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

using NewtonDynamics;

namespace WPFPhysics1
{
	public class EllipseBody : Body
	{
		private double width;
		private double height;

		public EllipseBody( CCollision collision, double width, double height )
			: base( collision, null )
		{
			this.width = width;
			this.height = height;
			offset.X = -width / 2;
			offset.Y = -height / 2;
		}

		/// <summary>
		/// Create instance
		/// </summary>
		/// <param name="world">World</param>
		/// <param name="width">Width</param>
		/// <param name="height">Height</param>
		/// <returns>New instance</returns>
		public static EllipseBody Create( CWorld world, double mass, double width, double height, double depth )
		{
			CCollisionConvexPrimitives collision = new CCollisionConvexPrimitives( world );
			collision.CreateCylinder( 0.5f, (float)depth, Matrix3D.Identity );

			// The cylinder is created with depth along the X axis.  We rotate around Y axis to move the depth into the Z axis.
			Matrix3D matrix = Matrix3D.Identity;
			matrix.Rotate( new Quaternion( new Vector3D( 0, 1, 0 ), 90 ) );
			matrix.Scale( new Vector3D( width, height, 1 ) );

			collision = AddConvexHullModifier( collision, matrix );

			EllipseBody body = new EllipseBody( collision, width, height );

			InertialMatrix inertialMatrix = collision.CalculateInertialMatrix;

			body.MassMatrix = new MassMatrix( (float)mass, inertialMatrix.m_Inertia * mass );
			body.CentreOfMass = inertialMatrix.m_Origin;

			collision.Release();

			return body;
		}

		public double Width { get { return width; } }
		public double Height { get { return height; } }
	}
}